<%
# We have to remove search form params before the url_for method call, otherwise it throughs it on
search_params = params[:search]
params.merge!(:search => nil)
href = url_for(params_for(:action => :update_table, :escape => false).delete_if{|k,v| k == 'search'})
-%>
<%= 
if request.xhr?
  form_remote_tag :url => href,
    :method => :get,
    :before => "addActiveScaffoldPageToHistory('#{href}', '#{params[:controller]}')",
    :after => "$('#{loading_indicator_id(:action => :search, :id => params[:id])}').style.visibility = 'visible'; Form.disable('#{search_form_id}');",
    :complete => "$('#{loading_indicator_id(:action => :search, :id => params[:id])}').style.visibility = 'hidden'; Form.enable('#{search_form_id}');",
    :failure => "ActiveScaffold.report_500_response('#{active_scaffold_id}')",
    :update => active_scaffold_content_id,
    :html => { :href => href, :id => search_form_id, :class => 'search' } 
else
  form_tag href,
     :method => :get
end
%>

  <ol class="form">
    <% active_scaffold_config.field_search.columns.each do |column| -%>
      <% name = "search[#{column.name}]" %>
      <% value = nil %>
      <% value = search_params[column.name] if search_params %>
      <li class="form-element">
        <dl>
          <dt>
            <label for="<%= "search_#{column.name}" %>"><%= column.label %></label>
          </dt>
        <dd>
        <%= case column.column.type
          when :boolean
            options = [
              ['', ''],
              [as_('True'), 1],
              [as_('False'), 0]
            ]
            select_tag name, options_for_select(options, value)
          when :integer, :decimal
            html = select_tag("#{name}[opt]", 
                options_for_select(['=', '>=', '<=', '>', '<', '!=', [as_('Between'), 'BETWEEN']]),
                :id => "search_#{column.name}_opt",
                :onchange => "$('search_#{column.name}_between').style.display = ($('search_#{column.name}_opt').value == 'BETWEEN')? '' : 'none';")
            html <<  "&nbsp;" + text_field_tag("#{name}[number]", (value || {})['number'], 
              :autocomplete => "off", :size => 10, :class => 'text-input', :id => "search_#{column.name}")
            html <<  "&nbsp;" + text_field_tag("#{name}[number1]", (value || {})['number1'], 
              :id => "search_#{column.name}_between", :style => "display:none",
              :autocomplete => "off", :size => 10, :class => 'text-input')
          else
            case column.form_ui
            when :multi_select
              options = options_for_association(column.association)
              select_tag(name+"[]", options_for_select(options, value), :size => 6, :multiple => true)
            when :select
              options = [[as_('- select -'), '']] + options_for_association(column.association)
              select_tag(name, options_for_select(options, value))
            when :calendar_date_select
              name = "search[#{column.name}]"
              from_value = to_value = nil
              if search_params && search_params["#{column.name}"]
                from_value = search_params["#{column.name}"]['from']
                to_value = search_params["#{column.name}"]['to']
              end
              custom_fields = "" << 
                calendar_date_select("search", column.name, 
                  {:name => "#{name}[from]", :size => 10, :value => from_value, :class => 'text-input', :id => "search_#{column.name}_from"}) <<
                " - " << 
                calendar_date_select("search", column.name, 
                  {:name => "#{name}[to]", :size => 10, :value => from_value, :class => 'text-input', :id => "search_#{column.name}_to"})

              trend_fields = as_("Recent") + " " << 
                text_field_tag("search[#{column.name}][number]", 1, :class => 'text-input', :size => 10) << " " << 
                select_tag("search[#{column.name}][unit]", 
                 options_for_select( ActiveScaffold::Finder::DateUnits.collect{|value, key| [as_(value), key]}, :day), 
                 :class => 'text-input')

              range_options = [ [as_("- select -"), ""]] + ActiveScaffold::Finder::DateRanges.collect{|value, key| [as_(value), key]}

              ranges = select_tag("#{name}[range]", options_for_select(range_options), :id => "search_#{column.name}_range", 
                :onchange => "$('search_#{column.name}_custom').style.display = ($('search_#{column.name}_range').value == 'CUSTOM')? '' : 'none';" +
                             "$('search_#{column.name}_trend').style.display = ($('search_#{column.name}_range').value == 'TREND')? '' : 'none'")
              ranges << "&nbsp;" + content_tag("span", trend_fields, :id => "search_#{column.name}_trend", :style => "display:none")
              ranges << "&nbsp;" + content_tag("span", custom_fields, :id => "search_#{column.name}_custom", :style => "display:none")
            else
              text_field_tag(name, value, { :autocomplete => "off", :size => 20, :class => 'text-input', :id => "search_#{column.name}"})
            end 
          end
        %>
        </dd>
      </dl>
    </li>
    <% end -%>
  </ol>
  <p class="form-footer">
    <%= submit_tag as_('Search'), :class => "submit" %>
    <a href="javascript:void(0)" class="cancel" onclick="f = this.up('form'); f.reset(); f.onsubmit();"><%= as_('Reset') -%></a>
    <%= loading_indicator_tag(:action => :search) %>
  </p>
</form>

<script type="text/javascript">
//<![CDATA[
  Form.focusFirstElement('<%= search_form_id -%>');
//]]>
</script>
